package com.webchat.ugc.repository.dao.mall;


import com.webchat.domain.dto.mall.CartShopDTO;
import com.webchat.ugc.repository.entity.mall.MallUserCartEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IMallUserCartDAO extends JpaRepository<MallUserCartEntity, Long> {


    int countByCreateByAndDeleted(String userId, Boolean deleted);

    MallUserCartEntity findAllByCreateByAndSkuId(String userId, Long skuId);

    @Query(value = "update MallUserCartEntity cart set cart.deleted = true " +
                   "where cart.createBy = :userId and cart.skuId = :skuId")
    int deleteAllByCreateByAndSkuId(String userId, Long skuId);

    @Query(value = "update MallUserCartEntity cart set cart.skuCount = :count " +
            "where cart.createBy = :userId and cart.skuId = :skuId")
    int updateUserCartSkuCount(String userId, Long skuId, Integer count);

    List<MallUserCartEntity> findAllByCreateByAndDeletedAndShopId(String userId, Boolean deleted, Long shopId);

    List<MallUserCartEntity> findAllByCreateByAndDeleted(String userId, Boolean deleted);

    @Query(value = "select new com.webchat.domain.dto.mall.CartShopDTO(cart.shopId, max(cart.updateDate)) " +
                   "from MallUserCartEntity cart " +
                   "where cart.createBy = :userId and cart.deleted = :deleted " +
                   "group by cart.shopId")
    List<CartShopDTO> findAllByUserCartShopId(String userId, Boolean deleted);
}
